//
//  NewSongViewController.m
//  GSG
//
//  Created by dllo on 15/4/28.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "NewSongViewController.h"
#import "AFNetworkingTool.h"
#import "AFNetworking.h"
#import "SongBookCell.h"
#import "SDCycleScrollView.h"
#import "MusicModel.h"
#import"SongModel.h"
#import "PlayerViewController.h"
#import"MBProgressHUD.h"
#import"PlayListViewController.h"
#import"singleSong.h"
@interface NewSongViewController ()<UITableViewDataSource, UITableViewDelegate, SDCycleScrollViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *tableArray;
@property (nonatomic, retain) NSMutableArray *tableArray1;
@property (nonatomic, retain) NSMutableArray *tableArray2;
@property (nonatomic, retain) NSMutableArray *Arr;
@property (nonatomic, copy) NSString *str;
@property (nonatomic, copy) NSString *string;
@property (nonatomic, retain) NSMutableArray *Arr1;
@property (nonatomic, copy) NSString *str1;
@property (nonatomic, copy) NSString *string1;
@property (nonatomic, retain) NSMutableArray *Arr2;
@property (nonatomic, copy) NSString *str2;
@property (nonatomic, copy) NSString *string2;
@property (nonatomic, retain) NSMutableArray *arraySection;
@property(nonatomic,copy)NSString *strPic;
@property(nonatomic,retain)UIImageView *viewPic;
@property(nonatomic,retain)NSMutableArray *arrURL;
@property(nonatomic,retain)NSMutableArray*arrModel;
@property(nonatomic,retain)NSMutableArray*arrModel1;
@property(nonatomic,retain)NSMutableArray*arrModel2;
@property(nonatomic,retain)MBProgressHUD *HUD;
@end

@implementation NewSongViewController

- (void)dealloc
{
    [_tableView release];
    [_Arr release];
    [_tableArray release];
    [_tableArray1 release];
    [_tableArray2 release];
    [_Arr1 release];
    [_Arr2 release];
    [_arraySection release];
    [_viewPic release];
    [_arrURL release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"新歌精选";
        self.tableArray = [NSMutableArray array];
        self.tableArray1 = [NSMutableArray array];
        self.tableArray2 = [NSMutableArray array];
        self.arrURL = [NSMutableArray array];
        self.Arr = [NSMutableArray array];
        self.arraySection=[NSMutableArray arrayWithObjects:@"华语",@"欧美",@"日韩",nil];
        self.Arr1 = [NSMutableArray array];
        self.Arr2 = [NSMutableArray array];
       
        self.arrModel=[[NSMutableArray alloc]init];
        self.arrModel1=[[NSMutableArray alloc]init];
        self.arrModel2=[[NSMutableArray alloc]init];
    
    }
    return self;
}


#pragma mark -
#pragma mark 创建TableView
- (void)createTableView
{
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bf.jpg"]];
    self.viewPic=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * 1 / 3)];
    // 网络加载 --- 创建带标题的图片轮播器
    SDCycleScrollView*cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * 1 / 3 - (self.view.frame.size.height * 40 / 667)) imageURLsGroup:nil];
    
    cycleScrollView2.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    cycleScrollView2.delegate=self;
    cycleScrollView2.dotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    [self.view addSubview:cycleScrollView2];
    cycleScrollView2.autoScrollTimeInterval = 4.0;
    cycleScrollView2.backgroundColor=[UIColor blackColor];
    
    //4.0 影响播放的效果 可以改变数字
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        cycleScrollView2.imageURLsGroup = self.arrURL;
    });
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.view.frame.size.height * 45 / 667) style:UITableViewStyleGrouped];
    self.tableView.tableHeaderView=cycleScrollView2;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.rowHeight = 100;
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //注册
    [self.tableView registerClass:[SongBookCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView release];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
#pragma mark -
#pragma mark 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

        if (section==0) {
            return _arrModel.count;
        }
        if (section==1) {
            return _arrModel1.count;
        }
        if (section==2) {
            return _arrModel2.count;
        }
        return 0;
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    SongBookCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[SongBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
    }
    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section==0) {
        SongModel*model = self.arrModel[indexPath.row];
     
        NSString *str = [NSString stringWithFormat:@"%ld.%@",indexPath.row +1, model.song_name];
        
        
        cell.song_nameLabel.text = str;
        cell.singer_nameLabel.text = model.singer_name;
    }
    if (indexPath.section==1) {
       SongModel *model = self.arrModel1[indexPath.row];
        NSString *str = [NSString stringWithFormat:@"%ld.%@",indexPath.row +1,model.song_name];
        cell.song_nameLabel.text = str;
        cell.singer_nameLabel.text = model.singer_name;
    }
    if (indexPath.section==2) {
        SongModel*model = self.arrModel2[indexPath.row];
        NSString *str = [NSString stringWithFormat:@"%ld.%@",indexPath.row +1,model.song_name];
        cell.song_nameLabel.text = str;
        cell.singer_nameLabel.text = model.singer_name;
    }
    
    return cell;
}

- (void)data
{
    NSString *str =@"http://v1.ard.q.itlily.com/share/user_timeline?msg_ids=%5B97249394%5D";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *params = [[NSDictionary alloc]init];
    [manager GET:str parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *arr = [dict objectForKey:@"data"];
        NSDictionary *dic = arr[0];
        NSMutableArray*arrPic=[dic objectForKey:@"pics"];
        NSString *str=arrPic[0];
        NSURL *url=[NSURL URLWithString:str];
        [self.arrURL  addObject:url];
        NSMutableArray *arr1 = [[[NSMutableArray alloc] init] autorelease];
        arr1 = [dic objectForKey:@"songlist"];
        for (int i = 0; i < arr1.count; i++) {
            self.str = [NSString stringWithFormat:@",%@", [arr1[i] objectForKey:@"song_id"]];
            [self.Arr addObject:self.str];
        }
        self.string = nil;
        NSString *lastString = @"";
        for (NSString *value in self.Arr) {
            self.string = [NSString stringWithFormat:@"%@%@", lastString, value];
            lastString = [NSString stringWithFormat:@"%@", self.string];
        }
        [self dataload];
       // [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"NO");
//        NSLog(@"%@", error);
    }];
}

- (void)dataload
{
    NSString *str = [NSString stringWithFormat:@"http://ting.hotchanson.com/songs/downwhite?song_id=%@",self.string];
 
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *arr = [dict objectForKey:@"data"];
//        for (NSInteger i = 0; i < arr.count; i++) {
//            MusicModel *model = [[[MusicModel alloc] init] autorelease];
//            NSMutableDictionary *modelDic = arr[i];
//            [model setValuesForKeysWithDictionary:modelDic];
//            [self.tableArray addObject:model];
//        }
        for(NSInteger i=0;i<arr.count ;i++){
            SongModel *song=[[SongModel alloc]init];
            NSMutableDictionary *dic=arr[i];
            song.singer_name=[dic objectForKey:@"singer_name"];
            song.song_name=[dic objectForKey:@"song_name"];
            
            NSMutableArray *arr1=[[NSMutableArray alloc]init];
            arr1=[dic objectForKey:@"url_list"];
          NSMutableDictionary *dic1=arr1[0];
            song.url=[dic1 objectForKey:@"url"];
                       [self.arrModel addObject:song];
        }
    
       
        
        //[self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
}
#pragma make 解析2
- (void)data1
{
    NSString *str =@"http://v1.ard.q.itlily.com/share/user_timeline?msg_ids=%5B97249154%5D";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *params = [[NSDictionary alloc]init];
    [manager GET:str parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *arr = [dict objectForKey:@"data"];
        NSDictionary *dic = arr[0];
        NSMutableArray*arrPic=[dic objectForKey:@"pics"];
        NSString *str=arrPic[0];
        NSURL *url=[NSURL URLWithString:str];
        [self.arrURL  addObject:url];
        NSMutableArray *arr1 = [[[NSMutableArray alloc] init] autorelease];
        arr1 = [dic objectForKey:@"songlist"];
        for (int i = 0; i < arr1.count; i++) {
            self.str1 = [NSString stringWithFormat:@",%@", [arr1[i] objectForKey:@"song_id"]];
            [self.Arr1 addObject:self.str1];
        }
        self.string1 = nil;
        NSString *lastString = @"";
        for (NSString *value in self.Arr1) {
            self.string1 = [NSString stringWithFormat:@"%@%@", lastString, value];
            lastString = [NSString stringWithFormat:@"%@", self.string1];
        }
        [self dataload1];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"NO");
//        NSLog(@"%@", error);
    }];
}

- (void)dataload1
{
    NSString *str = [NSString stringWithFormat:@"http://ting.hotchanson.com/songs/downwhite?song_id=%@",self.string1];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *arr = [dict objectForKey:@"data"];
//        for (NSInteger i = 0; i < arr.count; i++) {
//            MusicModel *model = [[[MusicModel alloc] init] autorelease];
//            NSMutableDictionary *modelDic = arr[i];
//            [model setValuesForKeysWithDictionary:modelDic];
//            [self.tableArray1 addObject:model];
//        }
        for(NSInteger i=0;i<arr.count ;i++){
            SongModel *song=[[SongModel alloc]init];
            NSMutableDictionary *dic=arr[i];
            song.singer_name=[dic objectForKey:@"singer_name"];
            song.song_name=[dic objectForKey:@"song_name"];
            
            NSMutableArray *arr1=[[NSMutableArray alloc]init];
            arr1=[dic objectForKey:@"url_list"];
           
            NSMutableDictionary *dic1=arr1[0];
            song.url=[dic1 objectForKey:@"url"];
          
            
            [self.arrModel1 addObject:song];
        }
        

        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
}

- (void)data2
{
    NSString *str =@"http://v1.ard.q.itlily.com/share/user_timeline?msg_ids=%5B97249108%5D";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *params = [[NSDictionary alloc]init];
    [manager GET:str parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *arr = [dict objectForKey:@"data"];
        NSDictionary *dic = arr[0];
        NSMutableArray*arrPic=[dic objectForKey:@"pics"];
        NSString *str=arrPic[0];
        NSURL *url=[NSURL URLWithString:str];
        [self.arrURL  addObject:url];
        NSMutableArray *arr1 = [[[NSMutableArray alloc] init] autorelease];
        arr1 = [dic objectForKey:@"songlist"];
        for (int i = 0; i < arr1.count; i++) {
            self.str2 = [NSString stringWithFormat:@",%@", [arr1[i] objectForKey:@"song_id"]];
            [self.Arr2 addObject:self.str2];
        }
        self.string2 = nil;
        NSString *lastString = @"";
        for (NSString *value in self.Arr2) {
            self.string2 = [NSString stringWithFormat:@"%@%@", lastString, value];
            lastString = [NSString stringWithFormat:@"%@", self.string2];
        }
        [self dataload2];
       [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"NO");
//        NSLog(@"%@", error);
    }];
}

- (void)dataload2
{
    NSString *str = [NSString stringWithFormat:@"http://ting.hotchanson.com/songs/downwhite?song_id=%@",self.string2];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *params = [[NSDictionary alloc]init];
    [manager GET:str parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *arr = [dict objectForKey:@"data"];
//        for (NSInteger i = 0; i < arr.count; i++) {
//            MusicModel *model = [[[MusicModel alloc] init] autorelease];
//            NSMutableDictionary *modelDic = arr[i];
//            [model setValuesForKeysWithDictionary:modelDic];
//            [self.tableArray2 addObject:model];
//        }
        for(NSInteger i=0;i<arr.count ;i++){
            SongModel *song=[[SongModel alloc]init];
            NSMutableDictionary *dic=arr[i];
            song.singer_name=[dic objectForKey:@"singer_name"];
            song.song_name=[dic objectForKey:@"song_name"];
            
            NSMutableArray *arr1=[[NSMutableArray alloc]init];
            arr1=[dic objectForKey:@"url_list"];
        NSMutableDictionary *dic1=arr1[0];
            song.url=[dic1 objectForKey:@"url"];
         
            
            [self.arrModel2 addObject:song];
        }
        

        [self.tableView reloadData];
       [_HUD hide:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"播放" style:UIBarButtonItemStyleDone target:self action:@selector(actionBuLand)];
     self.navigationItem.rightBarButtonItem.tintColor=[UIColor whiteColor];
    [self.view setBackgroundColor:[UIColor clearColor]];
    [self data];
    [self data1];
    [self data2];
    [self createTableView];
    //小菊花
    //方式1.直接在View上show
    _HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    
    
    //常用的设置
    //小矩形的背景色
    _HUD.color = [UIColor clearColor];//这儿表示无背景
    //显示的文字
    _HUD.labelText = @"提示";
    //细节文字
    _HUD.detailsLabelText = @"正在加载内容";
    //是否有庶罩
    _HUD.dimBackground = YES;

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arraySection.count;
}
- ( NSString *)tableView:( UITableView *)tableView titleForHeaderInSection:( NSInteger )section
{

    
    return self.arraySection[section];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PlayerViewController *pvc=[[PlayerViewController alloc]init];
    
    
    if (indexPath.section==0) {
        pvc.num=indexPath.row;
        pvc.arr=self.arrModel;
        
        [singleSong shareSingleSong].arrSong=self.arrModel;
        [singleSong shareSingleSong].num=indexPath.row;
        
        NSNotificationCenter *center=[NSNotificationCenter  defaultCenter];
        
        [center postNotificationName:@"播放" object:@"收到了看见了" userInfo:nil];

    }
    if (indexPath.section==1) {
        pvc.num=indexPath.row;
        pvc.arr=self.arrModel1;
        [singleSong shareSingleSong].arrSong=self.arrModel1;
        [singleSong shareSingleSong].num=indexPath.row;
        
        NSNotificationCenter *center=[NSNotificationCenter  defaultCenter];
        
        [center postNotificationName:@"播放" object:@"收到了看见了" userInfo:nil];

    }
    if (indexPath.section==2) {
        pvc.num=indexPath.row;
        pvc.arr=self.arrModel2;
        [singleSong shareSingleSong].arrSong=self.arrModel2;
        [singleSong shareSingleSong].num=indexPath.row;
        
        NSNotificationCenter *center=[NSNotificationCenter  defaultCenter];
        
        [center postNotificationName:@"播放" object:@"收到了看见了" userInfo:nil];

    }
   // [self.navigationController pushViewController:pvc animated:YES];
    
}
-(void)actionBuLand{
    PlayListViewController *plvc=[[PlayListViewController alloc]init];
    [self.navigationController pushViewController:plvc animated:YES];
    [plvc release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
